package com.moon.game.command.guest.market;

import java.util.List;

import com.moon.game.clazz.PlayerItemsProto.PlayerItems.PlayerItem;
import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Item;
import com.moon.game.service.ItemService;
import com.moon.game.service.PlayerItemService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;

/**
 * @description 购买道具
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-6-24 下午4:11:21
 */
public class BuyItemCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if(ps == null || ps.length < 2 || ps.length > 3) {
			doResponsePsError(req);
			return;
		}
		Integer buyId = Integer.parseInt(ps[0]);
		Integer num = Integer.parseInt(ps[1]);
		if (num == null || (num.intValue() < 1 || num.intValue() > Constants.ONCE_USE_ITEM_MAX_NUM)) {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_BEYOND_MAX_NUM, "请输入1~" + Constants.ONCE_USE_ITEM_MAX_NUM + "之间的数字！"));
			return;
		}
		if (buyId == null || buyId.intValue() == 0) {
			doResponsePsError(req);
			return;
		}
		final Item item = ItemMG.instance.getItem(buyId);
		if (item == null) {
			//资源不存在
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_RESOURCE, "你购买的物品不存在！"));
			return;
		}
		//检测是否是资源中可以被购买的道具
		final List<Integer> moneyGoods = ItemMG.instance.getMoneyGoods();
		final List<Integer> goldGoods = ItemMG.instance.getGoldGoods();
		if (!moneyGoods.contains(buyId) && !goldGoods.contains(buyId)) {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_BUY_ITEM, "您要购买的物品不存在！"));
			return;
		}
		
		String price = "";
		int buyMode = 2;//默认在买金币道具
		switch (item.getGoodsType()) {
			case Constants.COMMON_ITEM_TYPE://非卖品
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_RESOURCE, "您要购买的物品不存在！"));
				return;
			case Constants.GOLD_ITEM_TYPE://金币道具
				price = item.getGold() * num + "金币";
				break;
			case Constants.MONEY_ITEM_TYPE://碎银道具
				price = item.getMoney() * num + "碎银";
				buyMode = 1;
				break;
			default:
				break;
		}
		
		req.setVO("item_id", buyId);
		req.setVO("buy_mode", buyMode);
		if (ps.length == 2) {
			req.setVO("num", num);
			req.setVO("item_name", item.getName());
			req.setVO("price", price);
			doResult(req, "ack");
			return;
		}
		
		if (ps.length == 3) {
			if (Integer.parseInt(ps[2]) != 1) {
				doResponsePsError(req);
				return;
			}
		}
		PlayerVo player = getPlayerVo(req);
		//负重行囊空间检测
		boolean canBuy = false;
		if (item.getPosition() > 0) {
			canBuy = PlayerItemService.canAddPlayerItem(player.getSid(), player.getId(), item.getWeight());
			if (!canBuy) {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_BURDEN, "你负重不够" + item.getWeight() +",不能购买" + item.getName() + "×" + num + "！"));
				return;
			}
		}else {
			canBuy = ItemService.checkRoom(player.getSid(), player.getId(), num);
			if (!canBuy) {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_ROOM, "你行囊空间不够 "+ num +",不能购买" + item.getName() + "×" + num + "！"));
				return;
			}
		}
		
		boolean minusStatu = false;
		int type = 0;
		//金钱检测
		switch (item.getGoodsType()) {
			case Constants.COMMON_ITEM_TYPE://非卖品
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_RESOURCE, "您要购买的物品不存在！"));
				return;
			case Constants.MONEY_ITEM_TYPE://碎银道具
				minusStatu = PlayerService.minusMoney(player.getSid(), player.getId(), item.getMoney() * num);
				type = Constants.MONEY_ITEM_TYPE;
				break;
			case Constants.GOLD_ITEM_TYPE://金币道具
				minusStatu = PlayerService.minusGold(player.getSid(), player.getId(), item.getGold() * num);
				type = Constants.GOLD_ITEM_TYPE;
				break;
			default:
				break;
		}
		
		if (!minusStatu) {
			//钱币不足购买失败
			switch (type) {
				case Constants.MONEY_ITEM_TYPE:
					doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_MONEY, "你的碎银不足" + item.getMoney() * num +",不能购买" + item.getName() + "×" + num + "！"));
					return;
				case Constants.GOLD_ITEM_TYPE:
					doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_GOLD, "你的金币不足" + item.getGold() * num +",不能购买" + item.getName() + "×" + num + "！"));
					return;
				default:
					break;
			}
		}else {
			StringBuffer info = new StringBuffer();
			//扣除钱成功，添加道具
			if (item.getPosition() > 0) {
				//购买装备道具
				PlayerItem playerItem = PlayerItemService.playerItemFactory(buyId);
				PlayerItemService.addPlayerItem(player.getSid(), player.getId(), playerItem, true);//在上面已经有负重检测所以此处不需要返回值
			}else {
				//购买普通道具
				ItemService.addItem(player,buyId,num,true);
			}
			switch (type) {
				case Constants.MONEY_ITEM_TYPE:
					info.append("购买" + item.getName() + "×" + num + "成功，扣除" + item.getMoney() * num + "碎银。");
					break;
				case Constants.GOLD_ITEM_TYPE:
					info.append("购买" + item.getName() + "×" + num + "成功，扣除" + item.getGold() * num + "金币。");
					break;
				default:
					break;
			}
			req.setVO(TAG_INFO, info.toString());
		}
		
		response(req, req.getClientResponse());
	}

}
